import os, pydicom, csv
from tkinter.filedialog import askdirectory
from tqdm import tqdm

path_name = askdirectory()

with open('dicom.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['APR', 'Number of Slices', 'Completion'])
    print('Checking:')
    for root, dirs, files in tqdm(os.walk(path_name)):
        n_files = len(files)
        n_slices = 0
        if n_files and files[0].endswith('.dcm'):
            dcm = pydicom.read_file(os.path.join(root, files[0]))
            n_slices = dcm[(0x0054, 0x0081)].value
            check_complete = (n_files==n_slices)
        else:
            check_complete = 'No DICOM'

        writer.writerow([root, f'\'{n_files}/{n_slices}', check_complete])